package com.wanmait.recruitment.controller;

import com.github.pagehelper.PageInfo;
import com.wanmait.recruitment.pojo.Config;
import com.wanmait.recruitment.service.ConfigService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("manage/config")
public class ConfigListController {
    @Resource
    private ConfigService configService;
    @RequestMapping("list")
    public ModelAndView list(Config config, String pageNumber){
        ModelAndView modelAndView = new ModelAndView();
        int pageNum;
       if (config.getDescription()==""||config.getDescription()==null){

           try {
               pageNum = Integer.parseInt(pageNumber);
           } catch (Exception e) {
               pageNum = 1;
           }
           PageInfo<Config> pageInfo = configService.findAllConfig(config,pageNum,5);
           List<Config> configs = pageInfo.getList();
           modelAndView.addObject("pageInfo",pageInfo);
           return modelAndView;
       }
       else {
           try {
               pageNum = Integer.parseInt(pageNumber);
           } catch (Exception e) {
               pageNum = 1;
           }
           PageInfo<Config> pageInfo = configService.findAllConfig(config, pageNum, 12);
           List<Config> configs = pageInfo.getList();
           modelAndView.addObject("pageInfo", pageInfo);
           return modelAndView;
       }
    }
    @RequestMapping("update")
    public ModelAndView update(Integer id){
        ModelAndView modelAndView = new ModelAndView();
        Config config = configService.findById(id);
       modelAndView.addObject("config",config);
       modelAndView.setViewName("/manage/config/configDetailed");
       return modelAndView;
    }
    @RequestMapping("setValue")
    public String setValue(Config config){
        configService.update(config);
       return "redirect:/manage/config/list";
    }

}
